<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { ref } from 'vue'
import { setUserInfo } from '@/stores'
// 导入表单校验规则
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
// 导入接口函数
import { loginByPassword } from '@/services/user'
// 定义校验的节点
const mobile = ref('13230000089')
const password = ref('abc12345')
const argee = ref(false)
// pinia仓库实例对象
const store = setUserInfo()
// 路由核心对象
const router = useRouter()
const route = useRoute()
// console.log(router)

// 控制密码登录还是短信登录
const isPass = ref(true)
const code = ref('246810')
// 发送验证码的事件处理函数
const sending = ref(true)
const login = async () => {
  // 如果没有勾选协议
  if (!argee.value)
    return showToast({
      position: 'top',
      message: '请勾选协议'
    })
  const res = await loginByPassword(mobile.value, password.value)
  // console.log(res)
  // 拿到用户信息存储到pinia中
  store.setUser(res.data)
  showSuccessToast('登录成功')
  // 如果有完整路径 就跳转到对应完整路径上 如果没有 就跳转到个人主页
  // console.log(route.query.returnUrl)
  // console.log(router.options.history.location)
  // console.log(route.query.returnUrl)
  router.push((route.query.returnUrl as string) || '/user')
}
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      title="登录"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head" @click="isPass = !isPass">
      <h3>{{ isPass ? '密码登录' : '验证码登录' }}</h3>
      <a href="javascript:;">
        <span>{{ !isPass ? '密码登录' : '发送验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="login">
      <van-field
        placeholder="请输入手机号"
        type="tel"
        v-model="mobile"
        :rules="mobileRules"
      ></van-field>
      <van-field
        v-if="isPass"
        placeholder="请输入密码"
        type="password"
        v-model="password"
        :rules="passwordRules"
      ></van-field>
      <!-- 验证码登录 -->
      <van-field
        v-else
        placeholder="请输入验证码"
        v-model="code"
        :rules="codeRules"
      >
        <template #button>
          <van-button
            v-if="sending"
            round
            type="primary"
            size="small"
            @click="sending = false"
            >发送验证码</van-button
          >
          <van-count-down
            v-else
            :time="5000"
            format="发送验证码倒计时S秒"
            @finish="sending = true"
          />
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="argee">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" nativeType="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
